<?php 

//var_dump($_POST);exit;
$key = current(array_keys($_POST));

function checkUsername()
{
	$username = $_POST['username'];

	$preg = "/^[A-z0-9]{6,8}$/";

	$c = preg_match($preg, $username);

	if (!$c) {
		$arr = array(
				'error'=>1,
				'info'=>'长度以6-8位的字母或数字',
			);
		echo json_encode($arr);
		exit;
	}


	$users = include './userdb.php';

	foreach ($users as $v) {
		if ($username==$v['username']) {
			$arr = array(
			'error'=>1,
			'info'=>'用户名已存在',
		);
		echo json_encode($arr);
		exit;
		}
	}


	$arr = array(
			'error'=>0,
			'info'=>'用户名可用',
		);
	echo json_encode($arr);
	exit;
}

function checkEmail()
{
	$email = $_POST['email'];

	$preg = "/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/";

	$c = preg_match($preg, $email);

	if (!$c) {
		$arr = array(
				'error'=>1,
				'info'=>'邮箱格式不正确',
			);
		echo json_encode($arr);
		exit;
	}


	// $users = include './userdb.php';

	// foreach ($users as $v) {
	// 	if ($username==$v['username']) {
	// 		$arr = array(
	// 		'error'=>1,
	// 		'info'=>'用户名已存在',
	// 	);
	// 	echo json_encode($arr);
	// 	exit;
	// 	}
	// }


	$arr = array(
			'error'=>0,
			'info'=>'邮箱可用',
		);
	echo json_encode($arr);
	exit;
}

function checkPassword()
{
	$password = $_POST['password'];

	$preg = "/^[\w\W]{3,}$/";

	$c = preg_match($preg, $password);

	if (!$c) {
		$arr = array(
				'error'=>1,
				'info'=>'密码必须3位以上',
			);
		echo json_encode($arr);
		exit;
	}


	$arr = array(
			'error'=>0,
			'info'=>'密码可用',
		);
	echo json_encode($arr);
	exit;
}

function checkOther()
{
	$arr = array(
			'error'=>3,
			'info'=>'传入的参数不正确',
		);
	echo json_encode($arr);
	exit;
}

switch ($key) {
	case 'username':
		checkUsername();
		break;
	case 'email':
		checkEmail();
		break;
	case 'password':
		checkPassword();
		break;
	default:
		checkOther();
		break;
}










 ?>